/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jkoa.service;

import com.google.inject.Inject;
import com.jkoa.dao.MajikanDAO;
import com.jkoa.model.Majikan;
import com.wideplay.warp.persist.Transactional;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 *
 * @author wan.fairul
 */
public class MajikanService {

   @Inject MajikanDAO majikanDAO;

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    private final static Logger LOG = Logger.getLogger(MajikanService.class);
    private static boolean isDebug = LOG.isDebugEnabled();

    public List<Majikan> findAll(Map<String, String[]> param) {
        String query = "SELECT a FROM com.jkoa.model.Majikan a WHERE a.idMajikan is not null";
        if (isNotBlank(param.get("namaMajikan"))) {
            query += " AND namaMajikan LIKE :namaMajikan ";
        }
        Query q = sessionProvider.get().createQuery(query);
        if (isNotBlank(param.get("namaMajikan"))) {
            q.setString("namaMajikan", param.get("namaMajikan")[0].trim()+"%");
        }
        return q.list();
    }


    //Added By: mr5rule
     public Majikan saveMajikan(Majikan majikan) {
        return majikanDAO.saveOrUpdate(majikan);
    }
   

    public static boolean isNotBlank(String[] str) {
        return !isBlank(str);
    }

    public static boolean isBlank(String[] str) {
        if (str == null) {
            return true;
        }
        if (str.length > 0) {
            return isBlank(str[0]);
        }
        return true;
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
}
